package com.yingyi.user.controller;


import com.yingyi.basic.exception.BusinessException;
import com.yingyi.basic.util.JsonResult;
import com.yingyi.user.dto.BinderDto;
import com.yingyi.user.dto.LoginDto;
import com.yingyi.user.service.ILogInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogInService loginService;

    /**
     * 通过账号登录
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public JsonResult account(@RequestBody LoginDto loginDto){
        try {
            return loginService.account(loginDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("系统繁忙，请稍后重试");
        }

    }

    /**
     * 通过微信登录
     * @param code
     * @return
     */
    @GetMapping("/weChat/{code}")
    public JsonResult weChatLogin(@PathVariable(value = "code") String code){
        try {
            return loginService.weChatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("系统繁忙，请稍后重试");
        }

    }

    /**
     * 微信的账号与系统账号的绑定、登录
     * @param dto
     * @return
     */
    @PostMapping("/wechat/binder")
    public JsonResult weChatBinder(@RequestBody BinderDto dto){
        try {
            return loginService.weChatBinder(dto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("系统繁忙，请稍后重试");
        }
    }
}
